package edu.seu.mart.service.impl;

import edu.seu.mart.dao.IUserDao;
import edu.seu.mart.entity.Pager;
import edu.seu.mart.model.MartException;
import edu.seu.mart.model.User;
import edu.seu.mart.service.IUserService;
import edu.seu.mart.utils.SecurityUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by samsung on 2015/3/6.
 */
@Service
public class UserService implements IUserService {

    @Resource
    private IUserDao userDao;

    @Override
    public User login(String username, String password) {
        Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher matcher = pattern.matcher(username);
        if (matcher.matches()) {
            User user = userDao.findUserByEmail(username);
            if (user != null) {
                try {
                    if(!SecurityUtil.md5(user.getUsername(), password).equals(user.getPassword())) {
                        throw new MartException("邮箱或者密码不正确");
                    } else {
                        return user;
                    }
                } catch (NoSuchAlgorithmException e) {
                    throw new MartException("密码加密失败:"+e.getMessage());
                }
            } else {
                throw new MartException("邮箱不正确");
            }
        } else {
            User user = userDao.findUserByName(username);
            if (user != null){
                try {
                    if (!SecurityUtil.md5(username, password).equals(user.getPassword())) {
                        throw new MartException("用户名或者密码不正确");
                    } else {
                        return  user;
                    }
                } catch (NoSuchAlgorithmException e) {
                    throw new MartException("密码加密失败:"+e.getMessage());
                }
            } else {
                throw new MartException("用户名不正确");
            }
        }
    }

    @Override
    public Pager<User> findUser() {
        return userDao.findUser();
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    public void updateStatus(int id) {
        User u = userDao.load(id);
        if(u == null) throw new MartException("修改状态的用户不存在");
        if(u.getStatus() == 0) u.setStatus(1);
        else u.setStatus(0);
        userDao.update(u);
    }

    @Override
    public void deleteChecked(String[] ids) {
        try {
            for (String id : ids) {
                userDao.delete(Integer.valueOf(id));
            }
        } catch (Exception e) {
            throw new MartException("选中的用户删除失败");
        }
    }

    @Override
    public Pager<User> findUser(String name) {
        return userDao.findUser(name);
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

    @Override
    public void save(User user) {
        userDao.add(user);
    }

    @Override
    public void updateMoney(int id, double money) {
        User user = userDao.load(id);
        if (user != null) {
            user.setMoney(money);
            userDao.update(user);
        }
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User load(int id) {
        return userDao.load(id);
    }
}
